/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit.persistance;

import java.util.List;
import java.util.ArrayList;
import java.sql.*;

/**
 *
 * @author EuiHyun
 */
public class ParentDAO extends AbstractParentDAO {

    @Override
    public ParentDTO find(String parentId) throws PersistanceException {
        ParentDTO dto = null;
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select PARENTID, CHANGEUNITID, PARENTCHANGEUNITID from APP.PARENT where PARENTID = '"+parentId+"'";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            if(rs.next()) {
                dto = new ParentDTO();

                dto.setParentId(rs.getString("PARENTID"));
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setParentChangeUnitId(rs.getString("PARENTCHANGEUNITID"));
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ParentDAO.find: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dto; 
    }

    @Override
    public List<ParentDTO> findByChangeUnitId(String changeUnitId) throws PersistanceException {
        List<ParentDTO> dtoArray = new ArrayList<ParentDTO>();
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select PARENTID, CHANGEUNITID, PARENTCHANGEUNITID from APP.PARENT where CHANGEUNITID = '"+changeUnitId+"'";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                ParentDTO dto = new ParentDTO();
                dto.setParentId(rs.getString("PARENTID"));
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setParentChangeUnitId(rs.getString("PARENTCHANGEUNITID"));

                dtoArray.add(dto);
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ParentDAO.findByChangeUnitId: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dtoArray; 
    }

    @Override
    public List<ParentDTO> list() throws PersistanceException {
        List<ParentDTO> dtoArray = new ArrayList<ParentDTO>();
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select PARENTID, CHANGEUNITID, PARENTCHANGEUNITID from APP.PARENT ";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                ParentDTO dto = new ParentDTO();
                dto.setParentId(rs.getString("PARENTID"));
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setParentChangeUnitId(rs.getString("PARENTCHANGEUNITID"));

                dtoArray.add(dto);
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ParentDAO.list: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dtoArray; 
    }
    
    @Override
    public void insert(ParentDTO parentDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
//        String sql="insert into APP.PARENT (PARENTID, CHANGEUNITID, PARENTCHANGEUNITID ) " 
//                    + "values ('"+parentDTO.getParentId()+"','"+ parentDTO.getChangeUnitId() +"','"+ parentDTO.getParentChangeUnitId() +"')";
        
        String sql="insert into APP.PARENT (PARENTID, CHANGEUNITID, PARENTCHANGEUNITID ) " 
                    + "values (?,?,?)";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, parentDTO.getParentId());
            stmt.setString(2, parentDTO.getChangeUnitId());
            stmt.setString(3, parentDTO.getParentChangeUnitId());
            
            stmt.executeUpdate(); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ParentDAO.insert: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }
    }

    @Override
    public void update(ParentDTO parentDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
//        String sql="update APP.PARENT set PARENTID= '"+ parentDTO.getParentId()
//                + "' ,CHANGEUNITID= '"+ parentDTO.getChangeUnitId()
//                + "' ,PARENTCHANGEUNITID= '"+ parentDTO.getParentChangeUnitId()
//                + "' where PARENTID = '"+ parentDTO.getParentId() +"'";
        
        String sql="update APP.PARENT set PARENTID=?"
                + " ,CHANGEUNITID=?"
                + " ,PARENTCHANGEUNITID=?"
                + " where PARENTID =?";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, parentDTO.getParentId());
            stmt.setString(2, parentDTO.getChangeUnitId());
            stmt.setString(3, parentDTO.getParentChangeUnitId());
            stmt.setString(4, parentDTO.getParentId());
           
            stmt.executeUpdate(); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ParentDAO.update: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }   
    }

    @Override
    public void delete(String parentId) throws PersistanceException {
        Connection conn = null; 
        Statement stmt = null;  
        
        //Create SQL Statement
        String sql="delete from APP.PARENT "  
                   +"WHERE PARENTID = '"+parentId+"'";  
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            stmt.executeUpdate(sql); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ParentDAO.delete: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
    }

    @Override
    public void deleteByChangeUnitId(String changeUnitId) throws PersistanceException {
        Connection conn = null; 
        Statement stmt = null;  
        
        //Create SQL Statement
        String sql="delete from APP.PARENT "  
                   +"WHERE CHANGEUNITID = '"+changeUnitId+"'";  
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            stmt.executeUpdate(sql); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ParentDAO.deleteByChangeUnitId: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
    }

}
